package com.irdstudio.efp.console.api.rest;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.console.service.facade.SDicService;
import com.irdstudio.efp.console.service.vo.SDicListVO;
import com.irdstudio.efp.console.service.vo.SDicVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class SDicController extends AbstractController  {
	
	@Autowired
	@Qualifier("sDicService")
	private SDicService sDicService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/dics", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDicVO>> querySDicAll(
			@RequestBody SDicVO vo) {
		setUserInfoToVO(vo);
		List<SDicVO> outputVo = sDicService.queryAllOwner(vo);
		// 根据用户角色获取用户权限方案
		/**【不符合switch规范】
		 * String dataAuthority = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		List<SDicVO> outputVo = null;
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataAuthority);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sDicService.queryAllOwner(vo);
			break;
		【未实现方法】
		case CurrOrg:
			outputVo = sDicService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sDicService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sDicService.queryAllOwner(vo);
			break;
		}*/
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/dic", method=RequestMethod.GET)
	public @ResponseBody ResponseData<JSONObject> queryByPk(@RequestParam(value = "opttype",defaultValue = "") String opttype) {
		if (StringUtil.isEmpty(opttype))
		    return getResponseData(null);
		//判断是否为数组
		if (opttype.contains(",")){
            String[] strings = opttype.split(",");
            ArrayList<SDicListVO> outputVo = new ArrayList<SDicListVO>();
            for (String string : strings) {
				SDicListVO inVo = new SDicListVO();
                inVo.setOpttype(string);
                outputVo.add(inVo);
            }
            List<SDicListVO> sDicListVOS = sDicService.queryByopttypes(outputVo);
            JSONObject jsonObject = new JSONObject();
            if (sDicListVOS!=null)
            for (SDicListVO sDicListVO:sDicListVOS){
                jsonObject.put(sDicListVO.getOpttype(),sDicListVO.getsDicVOs());
            }
            else
                return getResponseData(null);
            return getResponseData(jsonObject);
        }
		else {
			SDicListVO inVo = new SDicListVO();
            inVo.setOpttype(opttype);
            List<SDicListVO> sDicListVOS = sDicService.queryByopttype(inVo);
            JSONObject jsonObject = new JSONObject();
            if (sDicListVOS!=null)
            for (SDicListVO sDicListVO:sDicListVOS){
                jsonObject.put(sDicListVO.getOpttype(),sDicListVO.getsDicVOs());
            }
            else
                return getResponseData(null);
            return getResponseData(jsonObject);
        }
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sDic
	 * @return
	 */
	@RequestMapping(value="/s/dic", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SDicVO inSDicVo) {		
		int outputVo = sDicService.deleteByPk(inSDicVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSDicVo
	 * @return
	 */
	@RequestMapping(value="/s/dic", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SDicVO inSDicVo) {
		int outputVo = sDicService.updateByPk(inSDicVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSDicVo
	 * @return
	 */
	@RequestMapping(value="/s/dic", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSDic(@RequestBody SDicVO inSDicVo) {
		int outputVo = sDicService.insertSDic(inSDicVo);
		return getResponseData(outputVo);
		
	}
}
